const app = getApp()
// pages/shop/hotel/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: null,
    tabs: ['住客评价', '酒店详情'],
    tabIndex: 0,
    room_list:[],
    room_info: null,
    start_date: '',
    start_date_arr: null,
    end_date: '',
    src: [],
    end_date_arr: null,
    days: 1,
    navigator: { hide: 0 },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中…',
    })
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })
    var that = this
    var start = new Date();
    var start_date = start.getFullYear() + "-" + (start.getMonth() + 1) + "-" + start.getDate();
    var start_date_arr = [start.getFullYear(), (start.getMonth() + 1), start.getDate()];
    var end = new Date(+start + 1000 * 60 * 60 * 24);
    var end_date = end.getFullYear() +"-"+ (end.getMonth() + 1) + "-" + end.getDate();
    var end_date_arr = [end.getFullYear(), (end.getMonth() + 1), end.getDate()];
    wx.request({
      url: 'https://www.58zltc.com/api.php/Shop/item/shop_id/' + options.shop_id + "/token/" + that.data.token,
      success: res => {
        this.setData({
          info: res.data,
          start_date: start_date,
          start_date_arr: start_date_arr,
          end_date: end_date,
          end_date_arr: end_date_arr
        })
        wx.setNavigationBarTitle({
          title: res.data.name
        })
        this.getRoomList(options.shop_id)
      }
    })
    //快速导航
    app.ihttpGet("https://www.58zltc.com/api.php/Index/getNav", function (res) {
      var v = that.data.navigator;
      v.info = res.data
      that.setData({
        navigator: v
      })
    })
    //屏幕宽度
    wx.getSystemInfo({
      success: function (res) {
        var width = res.screenWidth;
        var v = that.data.navigator;
        v.width = width
        that.setData({
          navigator: v
        })
      },
    })

    //实例化一个动画
    this.animation = wx.createAnimation({
      // 动画持续时间，单位ms，默认值 400
      duration: 300,
      /**
       * linear 动画一直较为均匀
       * ease  从匀速到加速在到匀速
       * ease-in 缓慢到匀速
       * ease-in-out 从缓慢到匀速再到缓慢
       * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过
       * step-end  保持 0% 的样式直到动画持续时间结束    一闪而过
       */
      timingFunction: 'ease',
      // 延迟多长时间开始
      delay: 100,
      /**
       * 以什么为基点做动画 效果自己演示
       * left,center right是水平方向取值，对应的百分值为left=0%;center=50%;right=100%
       * top center bottom是垂直方向的取值，其中top=0%;center=50%;bottom=100%
       */
      transformOrigin: 'right',
      success: function (res) {

      }
    })
    var width = this.data.navigator.width;
    var offset = width * 0.64;
    that.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = that.animation.export()
    v.hide = 1
    that.setData({
      //输出动画
      navigator: v
    })
  },
  getRoomList: function(shop_id){
    var that =this
    wx.request({
      url: 'https://www.58zltc.com/api.php/Hotel/roomList/shop_id/' + shop_id+"/token/"+that.data.token,
      success: res => {
        this.setData({
          room_list: res.data
        })
        wx.hideLoading()
      }
    })
  },
  longtap: function (e) {
    var that = this;
    var url = e.currentTarget.dataset.src;
    this.data.src.push(url);
    wx.previewImage({
      urls: that.data.src
    })
  },
  setTab: function (e) {
    console.log(e.currentTarget.dataset.index)
    this.setData({
      tabIndex: e.currentTarget.dataset.index
    })
  },
  getRoomInfo: function(e){
    const index = e.currentTarget.dataset.index;
    this.setData({
      room_info: this.data.room_list[index]
    })
  },
  hideRoomInfo: function(){
    this.setData({
      room_info: null
    })
  },
  showPics:function(e){
    const pics = e.currentTarget.dataset.pics;
    wx.previewImage({
      urls: pics,
    })
  },
  pay: function(e){
    const id = e.currentTarget.dataset.id;
    const data = this.data;
    // const start_date = data.start_date_arr[1] + "月" + data.start_date_arr[2] + "日";
    // const end_date = data.end_date_arr[1] + "月" + data.end_date_arr[2] + "日";
    wx.navigateTo({
      url: 'pay?shop_name=' + this.data.info.name + '&id=' + id + '&start_date=' + data.start_date + '&end_date=' + data.end_date,
    })
  },
  openLocation: function(e){
    const data = this.data.info;
    wx.openLocation({
      latitude: Number(data.latitude),
      longitude: Number(data.longitude),
      address: data.address,
      name: data.name
    })
  },
  setStartDate: function(e){
    const date = e.detail.value.split('-');

    this.setData({
      start_date: e.detail.value,
      start_date_arr: date
    })
    this.setDays();
  },
  setEndDate: function (e) {
    const date = e.detail.value.split('-');

    this.setData({
      end_date: e.detail.value,
      end_date_arr: date
    })
    this.setDays();
  },
  setDays: function(){
    const start = new Date(this.data.start_date);
    const end = new Date(this.data.end_date);
    const days = (end - start) / (1000 * 60 * 60 * 24);
    this.setData({
      days: days
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //快捷导航
  navigator: function (e) {
    var width = this.data.navigator.width
    if (this.data.navigator.hide == 1) {
      var offset = '0'
      var mum = 0;
    } else {
      var offset = width * 0.64
      var mum = 1;
    }
    this.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = this.animation.export();
    v.hide = mum;
    this.setData({
      //输出动画
      navigator: v
    })
  },
})